package mock;

import model.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: Student
 * Date: 30/11/12
 * Time: 10:13
 * To change this template use File | Settings | File Templates.
 */
public class MockGameState implements GameState {
    List<TeamState> teamStates;

    private static MockGameState mockGameState;

    public static MockGameState getInstance() {
        if (mockGameState == null) {
            mockGameState = new MockGameState();
        }
        return mockGameState;
    }

    public void roll() {
        for (TeamState teamState : teamStates) {
            teamState.roll();
        }
    }

    private MockGameState() {
        this.teamStates = new ArrayList<TeamState>();
        Random random = new Random();
        ConfigurableTeamState teamState = new ConfigurableTeamState();
        teamState.setColor(Color.green);
        teamState.setName("Green");
        teamStates.add(teamState);

        teamState.roll();

        teamState = new ConfigurableTeamState();
        teamState.setColor(Color.red);
        teamState.setName("Red");
        teamStates.add(teamState);

        teamState.roll();
    }


    @Override
    public List<TeamState> getTeamStates() {
        return teamStates;
    }

    @Override
    public void moveTask(Task task) {
        for (TeamState teamState : teamStates) {
            if (teamState.getDone().contains(task)) {
                if(teamState.decrementCapacity()) {
                    teamState.getDone().remove(task);
                    teamState.decrementCapacity();
                    task.resetUnitsOfWorkCompleted();
                    int indexOfCurrentTeam = teamStates.indexOf(teamState);
                    teamStates.get(indexOfCurrentTeam + 1).getBacklog().add(task);
                    task.resetUnitsOfWorkCompleted();
                    return;
                }
            } else {
                teamState.moveTask(task);
            }
        }
    }
}
